<template>
  <el-container>
    <el-header v-if="isAuthenticated">
      <el-menu
        :default-active="activeIndex"
        mode="horizontal"
        router
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <el-menu-item index="/users">用户管理</el-menu-item>
        <el-menu-item index="/roles">角色管理</el-menu-item>
        <el-menu-item index="/permissions">权限管理</el-menu-item>
        <el-menu-item index="/login" style="float: right" @click="handleLogout">退出登录</el-menu-item>
      </el-menu>
    </el-header>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, watch, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from './stores/userStore'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const activeIndex = ref('/users')
const isAuthenticated = computed(() => userStore.isAuthenticated)

watch(() => route.path, (newPath) => {
  activeIndex.value = newPath
}, { immediate: true })

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}
</script>

<style>
.el-container {
  height: 100vh;
}

.el-header {
  padding: 0;
}

.el-main {
  padding: 20px;
  background-color: #f5f7fa;
}
</style>
